package demo

import "sync"

// pkg/router/router.go
package router

type HandlerFunc func(*conn.TCPConn, proto.Message)

type Router struct {
	handlers sync.Map // map[uint32]HandlerFunc
}

// 注册带类型断言的安全路由
func (r *Router) Register(protocolID uint32, handler interface{}) error {
	switch h := handler.(type) {
	case func(*conn.TCPConn, *proto.LoginReq):
		r.handlers.Store(protocolID, func(c *conn.TCPConn, msg proto.Message) {
			if m, ok := msg.(*proto.LoginReq); ok {
				h(c, m)
			}
		})
	// 添加其他消息类型的case
	default:
		return errors.New("invalid handler type")
	}
	return nil
}